package br.com.fogas.medicao.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import br.com.fogas.database.FogasDatabase;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.medicao.business.SincronizacaoBusiness;
import br.com.fogas.medicao.custom.CircularProgressBar;
import br.com.fogas.webservice.service.FogasWebservice;
import butterknife.ButterKnife;
import butterknife.InjectView;

public class SincronizacaoActivity extends FragmentActivity {
	
	private int progressStatus = 0;
	
	@InjectView(R.id.circularprogressbar) CircularProgressBar progress;

    private Handler handler;
    
    private String imei;
    private String token;
    
    private FogasDatabase database;
    private FogasWebservice webservice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sincronizacao);
		ButterKnife.inject(this);
		
		this.imei = getIntent().getExtras().getString(Constants.IMEI);
		this.token = getIntent().getExtras().getString(Constants.TOKEN);
		
		this.database = new FogasDatabase(this);
		this.webservice = new FogasWebservice(this);
		
		this.handler = new Handler();
		
        new Thread(new Runnable() {
            public void run() {
            	SincronizacaoBusiness sincronizacao = new SincronizacaoBusiness(SincronizacaoActivity.this);
				sincronizacao.iniciar();
            }
        }).start();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.database.fecharConexao();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	public void atualizarProgressBar(final String title, final String subtitle, final int status) {
		handler.post(new Runnable() {
			public void run() {
				progress.setTitle(title);
				progress.setSubTitle(subtitle);
				progress.setProgress(status);
			}
		});
	}

	public CircularProgressBar getProgress() {
		return progress;
	}

	public void setProgress(CircularProgressBar progress) {
		this.progress = progress;
	}

	public int getProgressStatus() {
		return progressStatus;
	}

	public void setProgressStatus(int progressStatus) {
		this.progressStatus = progressStatus;
	}

	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public FogasDatabase getDatabase() {
		return database;
	}

	public void setDatabase(FogasDatabase database) {
		this.database = database;
	}

	public FogasWebservice getWebservice() {
		return webservice;
	}

	public void setWebservice(FogasWebservice webservice) {
		this.webservice = webservice;
	}
}